#ifndef CONNECTIONDIALOG_H
#define CONNECTIONDIALOG_H

#include <DDialog>
#include <DLineEdit>
#include <DComboBox>
#include <DSpinBox>
#include <DCheckBox>
#include <DTabWidget>
#include <QSqlDatabase>

DWIDGET_USE_NAMESPACE

class ConnectionDialog : public DDialog
{
    Q_OBJECT
public:
    explicit ConnectionDialog(QWidget *parent = nullptr);
    
    // 获取连接配置
    QString connectionName() const;
    QString driverName() const;
    QString hostName() const;
    int port() const;
    QString databaseName() const;
    QString userName() const;
    QString password() const;
    
    // 获取高级选项
    bool useSSL() const;
    QString sslCert() const;
    QString sslKey() const;
    QString sslCA() const;
    
    int connectTimeout() const;
    QString charset() const;
    bool autoReconnect() const;
    bool compressProtocol() const;
    
    // 设置现有连接配置进行编辑
    void setConnection(const QString &name, const QSqlDatabase &db);

private slots:
    void browseSSLCert();
    void browseSSLKey();
    void browseSSLCA();
    void browseDatabase();
    void updateSSLControls();
    void updateDefaultPort(const QString &driver);
    void testConnection();
    void saveAsTemplate();
    void loadTemplate();

private:
    void setupUI();
    void setupBasicTab(DTabWidget *tabWidget);
    void setupSSLTab(DTabWidget *tabWidget);
    void setupAdvancedTab(DTabWidget *tabWidget);
    void initDrivers();
    void loadSettings();
    void saveSettings();

    // Basic settings
    DLineEdit *nameEdit;
    DComboBox *driverCombo;
    DLineEdit *hostEdit;
    DSpinBox *portSpin;
    DLineEdit *dbNameEdit;
    DPushButton *browseDbBtn;
    DLineEdit *userEdit;
    DLineEdit *passwordEdit;
    
    // SSL settings
    DCheckBox *useSSLCheck;
    DLineEdit *sslCertEdit;
    DLineEdit *sslKeyEdit;
    DLineEdit *sslCAEdit;
    DPushButton *browseCertBtn;
    DPushButton *browseKeyBtn;
    DPushButton *browseCABtn;
    
    // Advanced settings
    DSpinBox *timeoutSpin;
    DComboBox *charsetCombo;
    DCheckBox *autoReconnectCheck;
    DCheckBox *compressCheck;
    
    // Template buttons
    DPushButton *saveTemplateBtn;
    DPushButton *loadTemplateBtn;
};

#endif // CONNECTIONDIALOG_H